 /*
 * @description: 抽奖活动列表
 * @Author: along
 * @Date: 2020-03-04
 * @Last Modified by: along
 * @Last Modified time: 2020-03-04
 */
<template>
    <div class="container luckDrawList">
        <!-- header -->
        <div class="header-nav">
            <div class="header-left">
                <div class="header-new-box">
                    <p class="header-new-box-title">活动信息:</p>
                    <el-input
                        v-model="formData.active_info"
                        placeholder="请输入内容"
                        size="small"
                        clearable
                        suffix-icon="el-icon-search"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">提交时间:</p>
                    <el-date-picker
                        v-model="date"
                        type="datetimerange"
                        :picker-options="pickerOptions()"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"
                        align="right"
                        size="small"
                        @change="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">活动状态:</p>
                    <el-select
                        v-model="formData.active_status"
                        clearable
                        filterable
                        placeholder="请选择"
                        size="small"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in activeList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="handleCreateLuck()"
                    >
                        新建活动
                    </el-button>
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="luckDrawList"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="lottery_no"
                label="活动编号"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="title"
                label="活动名称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="student_limit_type"
                label="参与用户"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    {{ scope.row.student_limit_type === 0 ? '所有用户' : scope.row.student_limit_type === 1 ? '新用户' : scope.row.student_limit_type === 2 ? '一卡通会员' : '' }}
                </template>
            </el-table-column>
            <el-table-column
                prop="create_time"
                label="创建时间"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="150"
            >
                <template slot-scope="scope">
                    {{ scope.row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                </template>
            </el-table-column>
            <el-table-column
                prop="start_time"
                label="活动有效期"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="180"
            >
                <template slot-scope="scope">
                    <p>自:{{ scope.row.start_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }} </p>
                    <p>至:{{ scope.row.end_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}</p>
                </template>
            </el-table-column>
            <el-table-column
                prop="status"
                label="活动状态"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    {{ scope.row.status | filterStatus }}
                </template>
            </el-table-column>
            <el-table-column
                prop="try_user_count"
                label="参与人数"
                show-overflow-tooltip
                align="center"
            />
            <el-table-column
                prop="try_count"
                label="参与次数"
                show-overflow-tooltip
                align="center"
            />
            <el-table-column
                prop="win_user_count"
                label="中奖人数"
                show-overflow-tooltip
                align="center"
            />
            <el-table-column
                prop="win_count"
                label="中奖次数"
                show-overflow-tooltip
                align="center"
            />
            <el-table-column
                prop="exchange_user_count"
                label="领奖人数"
                show-overflow-tooltip
                align="center"
            />
            <el-table-column
                prop="exchange_count"
                label="领奖次数"
                show-overflow-tooltip
                align="center"
            />
            <el-table-column
                label="操作"
                align="center"
                fixed="right"
                :width="290"
            >
                <template slot-scope="scope">
                    <div class="zsdx_button_luck">
                        <el-button
                            type="text"
                            class="curse-add"
                            @click="handleCheckLuck(scope.row)"
                        >
                            查看
                        </el-button>
                        <el-button
                            type="text"
                            class="curse-add"
                            @click="handleEditLuck(scope.row)"
                            v-if="scope.row.status === 0 || scope.row.status === 1 || scope.row.status === 2"
                        >
                            编辑
                        </el-button>
                        <el-button
                            type="text"
                            class="curse-add"
                            @click="handleUserLuck(scope.row)"
                            v-if="scope.row.status !== 0"
                        >
                            参与用户
                        </el-button>
                        <el-button
                            type="text"
                            class="curse-add"
                            @click="handleRecordLuck(scope.row)"
                            v-if="scope.row.status !== 0"
                        >
                            中奖记录
                        </el-button>
                        <el-button
                            type="text"
                            class="curse-add"
                            @click="handleCopyLuck(scope.row)"
                        >
                            复制
                        </el-button>
                        <el-button
                            type="text"
                            class="curse-add"
                            @click="handleDisabledLuck(scope.row)"
                            v-if="scope.row.status === 1 || scope.row.status === 2"
                        >
                            失效
                        </el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- /分页 -->
        <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>
    </div>
</template>

<script>
import { back } from '@/mixin/back';
import { getDate } from '@/assets/js/utils';
export default {
    mixins: [back],
    components: {
    },
    data () {
        return {
            formData: {
                active_info: '',
                active_status: 99
            },
            date: '',
            activeList: [{value: 99,label: '全部'},{value: 0,label: '草稿'},{value: 1,label: '未开始'},{value: 2,label: '进行中'},{value: 3,label: '已结束'},{value: -1,label: '失效'}],
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [],
            authResult: {}
        };
    },
    filters: {
        filterStatus (val) {
            if(val === 0) return '草稿';
            if(val === 1) return '未开始';
            if(val === 2) return '进行中';
            if(val === 3) return '已结束';
            if(val === -1) return '失效';
            return '--';
        }
    },
    computed: {},
    watch: {},
    mounted () {
        this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: '商城'},{title: '抽奖活动'},{title: '抽奖活动列表'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },
    methods: {
        /**
         * @description 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/shop_tool%5Clottery%5Cget_list', {
                page_id: page || this.page.now_page,
                lottery_keyword: this.formData.active_info,
                create_start_time: this.date === null ? '' : (this.date.length == 0 ? '' : getDate(new Date(this.date[0]))),
                create_end_time: this.date === null ? '' : (this.date.length == 0 ? '' : getDate(new Date(this.date[1]))),
                status: this.formData.active_status
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        type: 'warning',
                        duration: 2000
                    });
                }
            });
        },

        /**
         * @description 复制
         */
        handleCopyLuck (row) {
            this.$router.push({
                name: 'luckDrawEdit',
                query: {
                    no: row.lottery_no,
                    type: 'add',
                    status: '',
                    isCopy: 1
                }
            });
        },

        /**
         * @description 新建活动
         */
        handleCreateLuck () {
            this.$router.push({
                name: 'luckDrawEdit',
                query: {
                    no: '',
                    type: 'add',
                    status: ''
                }
            });
        },

        /**
         * @description 查看
         */
        handleCheckLuck (row) {
            this.$router.push({
                name: 'luckDrawEdit',
                query: {
                    no: row.lottery_no,
                    type: 'detail',
                    status: row.status
                }
            });
        },

        /**
         * @description 编辑
         */
        handleEditLuck (row) {
            this.$router.push({
                name: 'luckDrawEdit',
                query: {
                    no: row.lottery_no,
                    type: 'edit',
                    status: row.status
                }
            });
        },

        /**
         * @description 参与用户
         */
        handleUserLuck (row) {
            this.$store.commit('active/SET_ACTIVE_INFO', {
                try_user_count: row.try_user_count,
                try_count: row.try_count,
                exchange_count: row.exchange_count,
                exchange_user_count: row.exchange_user_count,
                win_count: row.win_count,
                win_user_count: row.win_user_count
            });

            this.$router.push({
                name: 'luckDrawUser',
                query: {
                    no: row.lottery_no
                }
            });
        },

        /**
         * @description 中奖记录
         */
        handleRecordLuck (row) {
            this.$store.commit('active/SET_ACTIVE_INFO', {
                try_user_count: row.try_user_count,
                try_count: row.try_count,
                exchange_count: row.exchange_count,
                exchange_user_count: row.exchange_user_count,
                win_count: row.win_count,
                win_user_count: row.win_user_count
            });

            this.$router.push({
                name: 'luckDrawRecord',
                query: {
                    no: row.lottery_no
                }
            });
        },

        /**
         * @description 失效奖品
         */
        handleDisabledLuck (row) {
            this.$post('/student/shop_tool%5Clottery%5Cdisable', {
                lottery_no: row.lottery_no
            }, resp => {
                if (resp.code == 1) {
                    this.getTableData();
                    this.$notify({
                        title: '提示',
                        message: '操作成功',
                        type: 'success',
                        duration: 2000
                    });
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        type: 'warning',
                        duration: 2000
                    });
                }
            });
        },

        /**
         * @description 时间处理
         */
        pickerOptions () {
            return {
                shortcuts : [{
                    text: '最近一周',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();

                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近一个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();

                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近三个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();

                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                        picker.$emit('pick', [start, end]);
                    }
                }]
            };
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 5px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            .header-left, .header-right {
                display: flex;
                align-items: center;
                flex-wrap: wrap;
            }
            .header-new-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                white-space: nowrap;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
    .zsdx_button_luck {
        display: flex;
        // flex-direction: column;
        align-items: center;
    }
</style>
<style lang="less">
    .luckDrawList{
        .el-table__body-wrapper .el-table__body .el-table__row td {
            border-right: none;
        }
        .el-table__header-wrapper .el-table__header .has-gutter tr th {
            border-right: none;
        }
        .el-table .el-table_1_column_6  .cell {
            position: relative;
        }
        .el-range-editor--small .el-range-separator{
            padding: 0 1px !important;
        }
        .el-button+.el-button {
            margin-left: 0px !important;
        }
        .zsdx_button_luck {
            .el-button {
                padding: 3px;
            }
        }
    }
</style>
